package function;

//验证码生成
public class fun3 {
    public static void main(String[] args) {
        char [] chs = new char[52];
        for (int i = 0; i < chs.length; i++) {
            chs[i] = (char)(i<=25?'A'+i:'a'+i-26);
        }
        int [] nums = new int[10];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = (int)(Math.random()*10);
        }
        //将两个数组合并成一个新的数组
        char [] newChs = new char[chs.length+nums.length];
        System.arraycopy(chs,0,newChs,0,chs.length);
        for (int i = 0; i < nums.length; i++) {
            newChs[chs.length + i] = (char)(nums[i] + '0');
        }
        String result = randomStr(chs,nums,newChs);
        System.out.println("随机生成的字符串为："+result);
    }

    public static String randomStr(char[] chs,int[] nums,char[] newChs) {
        StringBuilder result = new StringBuilder();
        result.append(chs[createIndex(chs.length)]);
        for (int i = 0; i < 3; i++) {
            int index = (int)(Math.random()*newChs.length);
            result.append(newChs[index]);
        }
        result.append(nums[createIndex(nums.length)]);
         return result.toString();
    }

    public static int createIndex(int num) {
        return (int)(Math.random()*num);
    }
}
